package com.edu.exam.entity.curriculum.questionbank;

import com.edu.exam.enums.QuestionAttachmentContentType;
import com.edu.exam.enums.QuestionAttachmentStatus;
import com.edu.exam.enums.QuestionAttachmentType;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.Comment;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.time.LocalDateTime;

/**
 * 试题附件实体类
 * 所属模块：B-02 题库管理模块
 * 对应数据库表：question_attachments
 *
 * 管理试题的附件文件信息，包括图片、音频、视频、文档等。
 * 支持附件的上传、下载、预览和管理。
 * 关联试题、创建人等关键信息，提供试题附件的完整视图。
 *
 * @author System
 * @version 1.0.0
 */
@Entity
@Comment("试题附件表")
@SQLDelete(sql = "UPDATE question_attachments SET is_deleted = true, delete_time = CURRENT_TIMESTAMP WHERE id = ?")
@Where(clause = "is_deleted = false")
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class QuestionAttachment {

    /**
     * 附件唯一标识符
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    
    private Long id;

    /**
     * 试题ID
     */
    @NotNull(message = "试题ID不能为空")
    @Column(name = "question_id", nullable = false)
    private Long questionId;

    /**
     * 附件名称
     */
    @NotBlank(message = "附件名称不能为空")
    @Size(max = 255, message = "附件名称长度不能超过255个字符")
    @Column(name = "attachment_name", nullable = false, length = 255)
    private String attachmentName;

    /**
     * 附件类型
     */
    @NotNull(message = "附件类型不能为空")
    @Enumerated(EnumType.STRING)
    @Column(name = "attachment_type", nullable = false, length = 20)
    private QuestionAttachmentType attachmentType;

    /**
     * 文件存储路径
     */
    @NotBlank(message = "文件路径不能为空")
    @Size(max = 500, message = "文件路径长度不能超过500个字符")
    @Column(name = "file_path", nullable = false, length = 500)
    private String filePath;

    /**
     * 文件原始名称
     */
    @Size(max = 255, message = "文件原始名称长度不能超过255个字符")
    @Column(name = "original_filename", length = 255)
    private String originalFilename;

    /**
     * 文件扩展名
     */
    @Size(max = 20, message = "文件扩展名长度不能超过20个字符")
    @Column(name = "file_extension", length = 20)
    private String fileExtension;

    /**
     * 文件大小（字节）
     */
    @Column(name = "file_size")
    private Long fileSize;

    /**
     * 文件MIME类型
     */
    @Size(max = 100, message = "MIME类型长度不能超过100个字符")
    @Column(name = "mime_type", length = 100)
    private String mimeType;

    /**
     * 文件内容类型
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "content_type", length = 20)
    private QuestionAttachmentContentType contentType;

    /**
     * 附件描述
     */
    @Size(max = 1000, message = "附件描述长度不能超过1000个字符")
    @Column(name = "description", length = 1000)
    private String description;

    /**
     * 附件状态
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "attachment_status", nullable = false, length = 20)
    private QuestionAttachmentStatus attachmentStatus;

    /**
     * 是否为必需附件
     */
    @Column(name = "is_required")
    @Builder.Default
    private Boolean isRequired = false;

    /**
     * 显示顺序
     */
    @Column(name = "display_order")
    @Builder.Default
    private Integer displayOrder = 0;

    /**
     * 缩略图URL
     */
    @Size(max = 500, message = "缩略图URL长度不能超过500个字符")
    @Column(name = "thumbnail_url", length = 500)
    private String thumbnailUrl;

    /**
     * 下载URL
     */
    @Size(max = 500, message = "下载URL长度不能超过500个字符")
    @Column(name = "download_url", length = 500)
    private String downloadUrl;

    /**
     * 预览URL
     */
    @Size(max = 500, message = "预览URL长度不能超过500个字符")
    @Column(name = "preview_url", length = 500)
    private String previewUrl;

    /**
     * 附件元数据，JSON格式
     */
    @Column(name = "metadata", columnDefinition = "TEXT")
    private String metadata;

    /**
     * 记录创建时间
     */
    @Column(name = "created_time", nullable = false, updatable = false)
    private LocalDateTime createdTime;

    /**
     * 记录最后更新时间
     */
    @Column(name = "updated_time", nullable = false)
    private LocalDateTime updatedTime;

    /**
     * 创建人用户ID
     */
    @Size(max = 50, message = "创建人长度不能超过50个字符")
    @Column(name = "created_by", nullable = false, length = 50)
    private String createdBy;

    /**
     * 最后更新人用户ID
     */
    @Size(max = 50, message = "更新人长度不能超过50个字符")
    @Column(name = "updated_by", length = 50)
    private String updatedBy;

    /**
     * 乐观锁版本号
     */
    @Version
    @Column(name = "version", nullable = false)
    @Builder.Default
    private Integer version = 1;

    /**
     * 软删除标记
     */
    @Column(name = "is_deleted", nullable = false)
    @Builder.Default
    private Boolean isDeleted = false;

    /**
     * 删除时间
     */
    @Column(name = "delete_time")
    private LocalDateTime deleteTime;

}